import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    server: {
      time: {
        val: '',
        loading: 1
      }
    },
    ent: {
      token: localStorage.getItem('trace-token') ? localStorage.getItem('trace-token') : '',
      ename: localStorage.getItem('trace-ename') ? localStorage.getItem('trace-ename') : '',
      etype: localStorage.getItem('trace-etype') ? localStorage.getItem('trace-etype') : ''
    },
    user_permit: {
      list: {
        1: { icon: 'el-icon-office-building', name: '主页', route: 'home' },
        2: { icon: 'el-icon-price-tag', name: '牲畜管理', route: 'animal' },
        3: { icon: 'el-icon-copy-document', name: '加工登记', route: 'proc' },
        4: { icon: 'el-icon-box', name: '订单管理', route: 'order' },
        5: { icon: 'el-icon-sell', name: '上架管理', route: 'stock' },
        6: { icon: 'el-icon-user', name: '员工管理', route: 'emp' },
        7: { icon: 'el-icon-setting', name: '账号管理' },
        '7-1': { name: '备案管理', route: 'lic' },
        '7-2': { name: '修改信息', route: 'prof' }
      }
    },
    admin_permit: {
      level: ['1', '2', '3'],
      list: {
        1: { icon: 'el-icon-office-building', name: '主页', route: 'home' },
        2: { icon: 'el-icon-price-tag', name: '企业账号管理', route: 'accInquiry' },
        3: { icon: 'el-icon-price-tag', name: '备案管理', route: 'record' }
      }
    },
    admin: {
      token: localStorage.getItem('trace-admin-token') ? localStorage.getItem('trace-admin-token') : '',
      name: localStorage.getItem('trace-admin-name') ? localStorage.getItem('trace-admin-name') : ''
    },
    gtype: {
      atype: {
        5001: '猪',
        5002: '牛',
        5003: '羊',
        5004: '鸡'
      },
      breed: {
        5001: {
          1: '大白猪',
          2: '太湖猪',
          3: '长白猪',
          4: '杜洛克猪'
        },
        5002: { 1: '家牛' },
        5003: { 1: '家羊' },
        5004: { 1: '家鸡' }
      },
      ptype: {
        5001: { 1: '整猪', 2: '猪蹄' },
        5002: { 1: '整牛' },
        5003: { 1: '整羊' },
        5004: { 1: '整鸡' }
      }
    },
    ent_type: {
      101: '养殖场',
      102: '屠宰场',
      103: '批发市场',
      104: '零售商'
    },
    lic_type: {
      1: '营业执照',
      2: '食品经营许可'
    }
  },
  getters: {
    permitLevel: state => {
      switch (1 * state.ent.etype) {
        case 101:
          return ['1', '2', '4', '6', ['7-1', '7-2']]
        case 102:
          return ['1', '3', '4', '6', ['7-1', '7-2']]
        case 103:
          return ['1', '4', ['7-1', '7-2']]
        case 104:
          return ['1', '4', '5', ['7-1', '7-2']]
        default:
          return []
      }
    }
  },
  mutations: {
    loadEntData (state, data) {
      state.ent = { ...data }
      localStorage.setItem('trace-token', data.token)
      localStorage.setItem('trace-ename', data.ename)
      localStorage.setItem('trace-etype', data.etype)
    },
    logout (state) {
      state.ent = {}
      localStorage.removeItem('trace-token')
      localStorage.removeItem('trace-ename')
      localStorage.removeItem('trace-etype')
    },
    setServerTimeLoading (state, s) {
      state.server.time.loading = s
    },
    setServerTime (state, t) {
      state.server.time.val = 1 * t
    },
    loadAdminData (state, data) {
      state.admin = { ...data }
      localStorage.setItem('trace-admin-token', data.token)
      localStorage.setItem('trace-admin-name', data.username)
    },
    adminLogout (state) {
      state.admin = {}
      localStorage.removeItem('trace-admin-token')
      localStorage.removeItem('trace-admin-name')
    }
  },
  actions: {},
  modules: {}
})
